using WCell.RealmServer.Entities;
using WCell.RealmServer.Misc;
using WCell.RealmServer.Spells;
using WCell.RealmServer.Spells.Auras;
using WCell.RealmServer.AI.Actions;

namespace WCell.RealmServer.AI.Brains
{
	/// <summary>
	/// TODO: Consider visibility of targets - Don't pursue target nor remove it from Threat list if not visible
	/// </summary>
	public class MobBrain : BaseBrain
	{
		#region Constructors

		public MobBrain(NPC owner)
			: base(owner)
		{
		}

		public MobBrain(NPC owner, BrainState defaultState)
			: base(owner, defaultState)
		{
		}

		public MobBrain(NPC owner, IAIActionCollection actions) : this(owner, actions, BrainState.Idle)
		{
		}

		public MobBrain(NPC owner, IAIActionCollection actions, BrainState defaultState) :
			base(owner, actions, defaultState)
		{
			
		}

		#endregion

		public override void OnHeal(Unit healer, Unit healed, int amtHealed)
		{
			if (m_owner is NPC && m_owner.IsInCombat && m_owner.CanBeAggroedBy(healer))
			{
				((NPC) m_owner).ThreatCollection[healer] += amtHealed/2;
			}
		}

		#region Event Handlers
		public override void OnLeaveCombat()
		{
			if (m_owner is NPC)
			{
				((NPC)m_owner).ThreatCollection.Clear();
			}
		}

		/// <summary>
		/// Called when owner received a debuff by the given caster
		/// </summary>
		/// <param name="caster"></param>
		/// <param name="cast"></param>
		/// <param name="debuff"></param>
		public override void OnDebuff(Unit caster, SpellCast cast, Aura debuff)
		{
			if (!m_IsRunning || caster == null)
				return;

			// TODO: How much threat do debuffs cause?
			if (m_owner is NPC && m_owner.CanBeAggroedBy(caster))
			{
				((NPC)m_owner).ThreatCollection[caster] += 1;
			}
		}

		/// <summary>
		/// Called whenever someone performs a harmful action on this Mob.
		/// </summary>
		/// <param name="action"></param>
		public override void OnDamageReceived(IDamageAction action)
		{
			if (!m_IsRunning)
				return;

			if (action.Attacker == null)
			{
				return;
			}

			if (m_owner is NPC && m_owner.CanBeAggroedBy(action.Attacker))
			{
				((NPC)m_owner).ThreatCollection[action.Attacker] += action.Attacker.GetGeneratedThreat(action);
			}
		}

		public override void OnCombatTargetOutOfRange()
		{

		}

		#endregion
	}
}